<template>
  <uni-nav-bar title="团成员列表" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package">
    <input class="uni-input" confirm-type="search" placeholder="搜索手机号/姓名" @confirm="confirm" v-model="search"
      @input="interrogate" />
  </view>
  <view class="package">{{TeamInfo.teamAll.name}}</view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package drive" v-for="item in orderLists" :key='item.id' style="padding-top:10rpx">
      <view class="flex-end"><uni-icons type="trash" size="28" @click="del(item.id)"></uni-icons></view>
      <view class="space-between top">
        <view>车友姓名</view>
        <view>{{item.name}}({{item.id}})</view>
      </view>
      <view class="space-between top">
        <view>账户余额</view>
        <view>{{item.money?item.money:'0.00'}}</view>
      </view>
      <view class="space-between top" v-if="TeamInfo.teamAll.type==2">
        <view>分拨余额</view>
        <view>{{item.fenbo?item.fenbo:0.00}}</view>
      </view>
      <view class="space-between top">
        <view>入团时间</view>
        <view>{{item.createtime}}</view>
      </view>
    </view>
  </scroll-view>
  <view class="add" @click="navTo('administrator/teamList/addDriver')">新增</view>
</template>

<script setup lang="ts">
  import { navBack, navTo } from '@/utils/navigator';
  import { toPublish } from '@mqtt';
  import { getTeamDriverList } from '@/gql/administrator';
  import { onShow } from '@dcloudio/uni-app';
  import { showLoading, errorToast, successToast } from '@/utils/prompt';
  import { ref } from 'vue';
  import { information } from '@/stores/administrator';
  const TeamInfo = information();
  /**
   * 团成员列表
   * @property {Number} lastPage 总页数
   * @property {Number} currentPage 当前页数
   * @property {String} search 搜索信息
   * @property {Boolean} inquireAbout 搜索状态
   * @property {Number} pageHeight 页面高度
   * @property {Array} orderLists 团成员列表
   */
  const lastPage = ref(1);
  const currentPage = ref(1);
  const orderLists = ref([]);
  const search = ref('')
  const inquireAbout = ref(false)
  const pageHeight = ref();
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 230;
    },
  });
  onShow(() => {
    currentPage.value = 1
    orderLists.value = []
    init()
  })
  /**
   * 获取团成员列表
   * @property {Number} page 当前页数
   * @property {Number} team_id 当前团ID
   * @property {String} keywords 搜索信息
   */
  function init() {
    showLoading()
    const payload = {
      query: getTeamDriverList,
      variables: {
        page: currentPage.value,
        team_id: Number(TeamInfo.teamAll.id),
        keywords: search.value
      },
    };
    toPublish(
      'ql/control/getTeamDriverList',
      payload,
      (obj : any) => {
        const { getTeamDriverList } = obj.data;
        orderLists.value = [...orderLists.value, ...getTeamDriverList.list];
        lastPage.value = getTeamDriverList.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  /**
   * 搜索内容
   */
  function interrogate(evt : any) {
    const { detail } = evt;
    if (!detail.value && inquireAbout) {
      console.log(detail.value)
      currentPage.value = 1
      orderLists.value = []
      init()
    }
  }
  function confirm() {
    if (search.value) {
      inquireAbout.value = search.value ? true : false;
      currentPage.value = 1
      orderLists.value = []
      init()
    } else {
      errorToast('请输入您要搜索的内容')
    }
  }
  function del(num : string) {
    showLoading('');
    const payload = {
      driver_id: Number(num),
      team_id: Number(TeamInfo.teamAll.id),
    };
    toPublish('control/delTeamDriver', payload, (obj : any) => {
      uni.hideLoading();
      const { code, msg } = obj;
      if (code === 1) {
        successToast(msg)
        orderLists.value = orderLists.value.filter((item) => item.id == num ? item = null : item)
      } else {
        errorToast(msg)
      }
    });
  }
</script>

<style scoped lang="less">
  .top {
    margin-top: 15rpx;
  }

  .drive:nth-of-type(1) {
    margin-top: 0px !important;
  }

  .add {
    width: 480rpx;
    height: 100rpx;
    background: linear-gradient(-10deg, #2C96F9, #1B91FF);
    border-radius: 5px;
    text-align: center;
    line-height: 100rpx;
    color: #fff;
    margin: 20rpx auto;
    font-size: 32rpx;
  }
</style>